package com.metaverse.backend.web;


import com.metaverse.backend.domain.User;
import com.metaverse.backend.dto.PageQuery;
import com.metaverse.backend.exception.BusinessException;
import com.metaverse.backend.repo.UserRepo;
import com.metaverse.backend.security.JwtTokenUtil;
import com.metaverse.backend.security.JwtUser;
import com.metaverse.backend.security.JwtUserFactory;
import com.metaverse.backend.service.UserService;
import com.metaverse.backend.utils.SecurityUtils;
import com.metaverse.backend.utils.excel.ExcelUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    private UserRepo userRepo;
    private UserService userService;
    private JwtTokenUtil jwtTokenUtil;



    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public User save(@RequestBody User user) {
        user = userService.addOrUpdate(user);
        return user;
    }

    @GetMapping("/my")
    public User my() {
        User user = userRepo.findById(SecurityUtils.getAuthenticatedUser().getId())
                .orElseThrow(new BusinessException("用户不存在"));
        return user;

    }


    @PostMapping("/all")
    public Page<User> all(@RequestBody PageQuery pageQuery) {
        JwtUser user = currentUser();
        if (!(user != null
                && user.isAdmin())) {
            pageQuery.getQuery().put("hasRole", "ROLE_MINTER");
        }
        return userService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public User get(@PathVariable Long id) {
        return userRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }



    @PostMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
        List<User> data = userService.all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }




    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/setPasswordAdmin")
    public String setPasswordAdmin(@RequestParam Long userId, @RequestParam String password) {
        return userService.setPassword(userId, password);
    }




    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/getToken/{userId}")
    public String getToken(@PathVariable Long userId) {
        User user = userRepo.findById(userId)
                .orElseThrow(new BusinessException("用户不存在"));
        return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/update")
    public User update(@RequestBody User user) {
        return userService.updateData(user);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/del")
    public void del( @RequestParam Long id) {
        userService.deleteById(id);
    }














}


